WPF将Width绑定到Parent.Width*0.3

 kingsharedn_594 发布于 2023-02-13 14:16

我想将控件的宽度绑定到父级的宽度,但是达到一定的比例.有没有办法做这样的事情:


Somedust.. 16

当然,但您需要使用转换器.像这样的东西:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace WpfTestBench.Converters
{
    public class PercentageConverter : MarkupExtension, IValueConverter
    {
        private static PercentageConverter _instance;

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return _instance ?? (_instance = new PercentageConverter());
        }
    }
}

你的XAML看起来像:


    
        
    

请注意,"使用ToDouble(String)方法等效于将值传递给Double.Parse(String)方法,使用_current线程culture_"的格式约定来解释值. (2认同)


小智.. 5

我建议只使用网格列和*宽度类型在XAML中执行此操作:


    
        
            
            
        

        
         

    

您可以通过更改列宽中*之前的数字来更改列占用的比率.这里设置为1和2,因此网格将分为3(所有*宽度的总和),宽度的1/3到第一列,2/3到第二列.

2 个回答
  • 当然,但您需要使用转换器.像这样的东西:

    using System;
    using System.Globalization;
    using System.Windows.Data;
    using System.Windows.Markup;
    
    namespace WpfTestBench.Converters
    {
        public class PercentageConverter : MarkupExtension, IValueConverter
        {
            private static PercentageConverter _instance;
    
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    
            #endregion
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                return _instance ?? (_instance = new PercentageConverter());
            }
        }
    }
    

    你的XAML看起来像:

    <Window x:Class="WpfTestBench.ScaleSample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:converters="clr-namespace:WpfTestBench.Converters"
            Title="Scale sample"  >
        <Grid Name="ParentGrid">
            <Rectangle
                0,5'}"
                Stroke="Black" StrokeThickness="2" />
        </Grid>
    </Window>
    

    2023-02-13 14:20 回答
  • 我建议只使用网格列和*宽度类型在XAML中执行此操作:

    <Window x:Class="NameSpace.WindowName"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1"  >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  />
                <ColumnDefinition  />
            </Grid.ColumnDefinitions>
    
            <Grid Grid.Column="0"></Grid><!--This item take up 1/3 of window width-->
            <Grid Grid.Column="1"></Grid> <!--This item take up remaining 2/3 of window width-->
    
        </Grid>
    </Window>
    

    您可以通过更改列宽中*之前的数字来更改列占用的比率.这里设置为1和2,因此网格将分为3(所有*宽度的总和),宽度的1/3到第一列,2/3到第二列.

    2023-02-13 14:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有