English 中文(简体)
视事实而定的区别
原标题:Binding a Distinction basedon Actual Width to Visibility

Is it possible to bind an ActualWidth or Width property of a Control to the Visibility of another Control with a distinction about value (like <200)? In my Opinion it is only possible with a converter because a DataTrigger can not work with > or <.

因此,我与一位老虎一起审判,但却没有工作。 我不敢确定需要哪些类型的转换器,以及哪一种转换器需要这种解决办法。

x

<StackPanel>
    <Slider x:Name="slider" Height="36" Width="220" Maximum="500"/>
    <Rectangle x:Name="mover"  Height="12" Stroke="Black" Width="{Binding Value, ElementName=slider}"/>
    <Rectangle x:Name="rectangle" Fill="#FFFF9E0E" Height="34" Width="112" Visibility="{Binding ActualWidth, Converter={StaticResource umkehr}, ElementName=rectangle, Mode=OneWay}"/>
</StackPanel>

以及兑换人的想法:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
   if (value != null) {
       var val = System.Convert.ToDouble(value);
        if (val > 100)
            return Visibility.Visible;
        return Visibility.Collapsed;
    }
        return Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}
问题回答

It is likely not w或king because you are binding your Rectangle s Visibility to the same rectangle s ActualWidth, and an invisible rectangle will always have a width of 0, so will never be visible.

Here s some examples. One binds to the other rectangle s ActualWidth, and the other binds to your Slider s Value

<Rectangle x:Name="rectangle" 
           Visibility="{Binding ActualWidth, ElementName=mover, 
               Converter={StaticResource umkehr}}"/>

<Rectangle x:Name="rectangle" 
           Visibility="{Binding Value, ElementName=slider, 
               Converter={StaticResource umkehr}}"/>

And as far as I know, there s no easy way of basing a value off of if something is greater than 或 less than a value. Coverters are your best option.

<代码>ActualWidth 是按框架类别分列的仅有的财产——

public double ActualWidth { get; }

It is get only property hence you can t set it to other value from code. You can bind to Width of your control instead to make it work.

http://www.ohchr.org。

对我来说,这可能是你想要的——

<><>载荷>

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value is double)
    {
       return ((double)value > 100) ? Visibility.Visible : Visibility.Collapsed;
    }
    return Visibility.Collapsed;
}

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

XAML

<StackPanel>
   <Slider x:Name="slider" Height="36" Width="220" Maximum="500"/>
   <Rectangle x:Name="mover"  Height="12" Stroke="Black" Width="{Binding Value, ElementName=slider}"/>
    <Rectangle x:Name="rectangle" Fill="#FFFF9E0E" Height="34" Width="112" Visibility="{Binding ActualWidth, Converter={StaticResource MyConverter}, ElementName=mover, Mode=OneWay}"/>
</StackPanel>

如果你需要:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double cutoff = 0.0;

            if(parameter is double)
            {
                cutoff = (double)parameter;
            }

            if (parameter is string)
            {
                Double.TryParse(parameter.ToString(), out cutoff); 
            }

            if (value is double)
            {
                return ((double)value > cutoff) ? Visibility.Visible : Visibility.Collapsed;
            }
            return Visibility.Collapsed;
        }

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

并且:

<StackPanel>
    <Slider x:Name="slider" Height="36" Width="220" Maximum="500"/>
    <Rectangle x:Name="mover"  Height="12" Stroke="Black" Width="{Binding Value, ElementName=slider}"/>
     <Rectangle x:Name="rectangle" Fill="#FFFF9E0E" Height="34" Width="112" 
                Visibility="{Binding ActualWidth, Converter={StaticResource ActualWidthToVisibilityConverter},                                                                                               
                ElementName=mover, Mode=OneWay, ConverterParameter=100}"/>
</StackPanel>




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...