Skip to main content
 首页 » 编程设计

c#之元素绑定(bind)问题

2024年02月20日13sxdcgaq8080

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
x:Class="Trainning.biding" 
x:Name="Window" 
Title="biding" 
Width="640" Height="480"> 
<Window.Resources> 
    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"> 
        <Setter Property="Background" Value="Transparent"/> 
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
        <Setter Property="Padding" Value="2,0,0,0"/> 
    </Style> 
</Window.Resources> 
<Grid Margin="8,8,0,8" DataContext="{Binding Source={StaticResource kirupaZuneDataSampleDataSource}}"> 
    <ComboBox x:Name="comboBox" Height="48" Margin="99,83,0,0" VerticalAlignment="Top" SelectedIndex="0" HorizontalAlignment="Left" Width="96"> 
        <ComboBoxItem Content="#FF13428D"/> 
        <ComboBoxItem Content="#00FFFFFF" Background="#00166A13"/> 
        <ComboBoxItem Content="#FF890E6F" Background="#0005116E"/> 
        <ComboBoxItem Content="#0005116E" Background="#0009683A"/> 
        <ComboBoxItem Content="##0009683A"/> 
    </ComboBox> 
    <TextBox Margin="99,145,297,0" TextWrapping="Wrap" Text="{Binding SelectedItem, ElementName=comboBox}" Height="41" VerticalAlignment="Top"/> 
</Grid> 

输出结果如下图

但我确实需要将字符串“#FF13428D”绑定(bind)到文本框中。

我该怎么做...请帮助我..

请您参考如下方法:

使用

Text="{Binding SelectedItem.Content, ElementName=comboBox}"  

访问所选组合框项目的内容。