Got some problem with wpf multibinding
-
Hi, I have some problem withe multi binding in wpf: 1) In the widnow cs file, I define a property like this:
private ObservableCollection<string> colltest = new ObservableCollection<string>(); public ObservableCollection<string> Coll { get { return this.colltest; } }
-
In the xaml file define a multi binding to the "togglebutton".
<ToggleButton Tag="dd" Content="TT" Width="50" Height="50">
<ToggleButton.IsChecked>
<MultiBinding Mode="OneWay" Converter="{x:Static app:SimpleConverter.Instance}">
<Binding Mode="OneWay" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type app:Window1}}"
Path="Coll" />
<Binding Mode="OneWay" RelativeSource="{RelativeSource Mode=Self}" Path="Tag" />
</MultiBinding>
</ToggleButton.IsChecked>
</ToggleButton> -
and the covnert function of the converter is like this:
public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bflag = false;if (value[0] == DependencyProperty.UnsetValue || value[0] == null||
value[1] == DependencyProperty.UnsetValue ||
value[1] == null)
return bflag;ObservableCollection<string> v1 = value[0] as ObservableCollection<string>;
string v2 = (string)value[1];if (v1 != null)
{
bflag = v1.Contains(v2);
}return bflag;
}
It's working after the window is loadded. But however, after I add some new string to the collection "colltest", the togglebutton's "IsChecked" is not updated automatically. I want it working in this way: If any changes are made to the collection "colltest". The togglebutton's "IsChecked" could update accordingly. So how to resolve this problem? thanks.
-
-
Hi, I have some problem withe multi binding in wpf: 1) In the widnow cs file, I define a property like this:
private ObservableCollection<string> colltest = new ObservableCollection<string>(); public ObservableCollection<string> Coll { get { return this.colltest; } }
-
In the xaml file define a multi binding to the "togglebutton".
<ToggleButton Tag="dd" Content="TT" Width="50" Height="50">
<ToggleButton.IsChecked>
<MultiBinding Mode="OneWay" Converter="{x:Static app:SimpleConverter.Instance}">
<Binding Mode="OneWay" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type app:Window1}}"
Path="Coll" />
<Binding Mode="OneWay" RelativeSource="{RelativeSource Mode=Self}" Path="Tag" />
</MultiBinding>
</ToggleButton.IsChecked>
</ToggleButton> -
and the covnert function of the converter is like this:
public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bflag = false;if (value[0] == DependencyProperty.UnsetValue || value[0] == null||
value[1] == DependencyProperty.UnsetValue ||
value[1] == null)
return bflag;ObservableCollection<string> v1 = value[0] as ObservableCollection<string>;
string v2 = (string)value[1];if (v1 != null)
{
bflag = v1.Contains(v2);
}return bflag;
}
It's working after the window is loadded. But however, after I add some new string to the collection "colltest", the togglebutton's "IsChecked" is not updated automatically. I want it working in this way: If any changes are made to the collection "colltest". The togglebutton's "IsChecked" could update accordingly. So how to resolve this problem? thanks.
Eric Vonjacson wrote:
If any changes are made to the collection "colltest". The togglebutton's "IsChecked" could update accordingly.
What kind of changes? ObservableCollection notifies about changes to the collection, but if you want notifications on changes to items in the collection, you'll need to implement INotifyPropertyChanged on those items (the item's class).
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
-
Hi, I have some problem withe multi binding in wpf: 1) In the widnow cs file, I define a property like this:
private ObservableCollection<string> colltest = new ObservableCollection<string>(); public ObservableCollection<string> Coll { get { return this.colltest; } }
-
In the xaml file define a multi binding to the "togglebutton".
<ToggleButton Tag="dd" Content="TT" Width="50" Height="50">
<ToggleButton.IsChecked>
<MultiBinding Mode="OneWay" Converter="{x:Static app:SimpleConverter.Instance}">
<Binding Mode="OneWay" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type app:Window1}}"
Path="Coll" />
<Binding Mode="OneWay" RelativeSource="{RelativeSource Mode=Self}" Path="Tag" />
</MultiBinding>
</ToggleButton.IsChecked>
</ToggleButton> -
and the covnert function of the converter is like this:
public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bflag = false;if (value[0] == DependencyProperty.UnsetValue || value[0] == null||
value[1] == DependencyProperty.UnsetValue ||
value[1] == null)
return bflag;ObservableCollection<string> v1 = value[0] as ObservableCollection<string>;
string v2 = (string)value[1];if (v1 != null)
{
bflag = v1.Contains(v2);
}return bflag;
}
It's working after the window is loadded. But however, after I add some new string to the collection "colltest", the togglebutton's "IsChecked" is not updated automatically. I want it working in this way: If any changes are made to the collection "colltest". The togglebutton's "IsChecked" could update accordingly. So how to resolve this problem? thanks.
- If it's a problem setting up the data binding, the error will NOT display like an exception, but will show up in the Output window. Open that up and watch for any messages. That should let you know if it's finding the binding source. 2) If that much is working, drop a breakpoint in your Convert routine, and step through to see if the flag is being updated properly.
-